<?php

namespace App\Http\Controllers\Innerend;


use App\Models\User;
use App\Models\UserGold;

class UserGoldController extends BaseController
{
    // 成员管理
    public function initialize()
    {
        parent::initialize();

        $uid = $this->getData('uid', 0, 'intval');
        $url = url('/innerend/user-gold/lists?uid='.$uid);

        $this->assign('init_url', $url);
        $this->model = 'UserGold';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作


    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return ['list' => $lists, 'tr' => $trList];
        }

        $userDb = new User();
        $uids   = array_column($lists, 'uid');
        $users  = $userDb->isCache(true)->getListInIds($uids);

        // 展示列表表格
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_uid  = $item['uid'];
            $_user = $users[$_uid] ?? [];

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($_user['username']??'');
            $tr .= $listHtml->list_td($item['title']);
            $tr .= $listHtml->list_td($item['price']);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return ['list' => $lists, 'tr' => $trList];
    }

    // 列表字段
    protected function list_fields()
    {
        return ['uid' => '用户', 'title' => '标题', 'price' => '数量', 'ctime' => '时间'];
    }


    // 表单页面
    protected function formTable()
    {
        $html = '';
        $info = $this->onShow();
        $form = $this->adminForm();

        // 用户名
        //$role     = $form->formCheckbox('role', json_decode($info['role']), $all); // 角色
        $username = $form->formInput('username', $info['username'] ?? '', '用户名');
        $username = $form->formInput('uid', $info['uid'] ?? '', '用户名');

        $username = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        $is_major = $form->formRadio('is_major', $info['is_major'] ?? 1, [1 => '重要', 0 => '不重要']);
        $title    = $form->formInput('title', $info['title'] ?? '', '标题');
        $content  = $form->formTextarea('content', $info['content'] ?? '');

        $html .= $form->makeItem('编辑', $username);
        $html .= $form->makeItem('重要性', $is_major);
        $html .= $form->makeItem('标题', $title);
        $html .= $form->makeItem('内容', $content);

        return $html;
    }

    // 列表搜索
    protected function listWhere()
    {
        $where = [];
        $uid   = $this->getData('uid', 0, 'intval');
        $field = $this->getData('field', 0, 'intval');
        if($uid){
            $where['uid'] = $uid;
        }
        if($field){
            $where['field'] = $field;
        }

        return $where;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = url('/innerend/'.$this->controller.'/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加</a> ';
        $this->assign('top_button', '');
    }

    // 列表搜索
    protected function list_search()
    {
        $request = $this->getData();
        $form    = $this->adminList();
        $keyword = $form->formInput('keyword', $request['keyword'] ?? '', '关键字');
        $search  = ['keyword' => $keyword,];  // keyword
        $this->assign('search', []);

        $searchHide = ['uid'];
        $this->assign('searchHide', $searchHide);
    }

    // 添加
    protected function old_before_add()
    {
        $data = [
            'uid'   => $this->getData('uid', 0, 'intval'),
            'date'  => $this->getData('date', 0, 'intval'),
            'title' => $this->getData('title', null, 'trim'),
            'field' => $this->getData('field', null, 'trim'),
            'price' => $this->getData('price', 0, 'intval'),
        ];

        // 检查唯一性
        $model = new User();
        $data  = $model->checkUser($data, $this->id);

        return $data;
    }

    // 修改
    protected function old_before_edit()
    {
        $data = [
            'uid'   => $this->getData('uid', 0, 'intval'),
            'date'  => $this->getData('date', 0, 'intval'),
            'title' => $this->getData('title', null, 'trim'),
            'field' => $this->getData('field', null, 'trim'),
            'price' => $this->getData('price', 0, 'intval'),
        ];

        // 检查唯一性
        $model = new User();
        $data  = $model->checkUser($data, $this->id);

        return $data;
    }

}


